<template>
  <el-form
    v-loading="loading"
    element-loading-text="正在注册..."
    element-loading-spinner="el-icon-loading"
    element-loading-background="rgba(0, 0, 0, 0.8)"
    :rules="rules"
    ref="loginForm"
    :model="loginForm"
    class="loginContainer"
  >
    <h3 class="loginTitle">欢迎来到blog，请注册</h3>
    <el-form-item prop="username">
      <el-input
        prefix-icon="el-icon-user-solid"
        type="text"
        auto-complete="false"
        v-model="loginForm.username"
        placeholder="请输入用户名"
      ></el-input>
    </el-form-item>
    <el-form-item prop="password">
      <el-input
        prefix-icon="el-icon-view"
        type="password"
        auto-complete="false"
        v-model="loginForm.password"
        placeholder="请输入密码"
      ></el-input>
    </el-form-item>
    <el-form-item prop="nickname">
      <el-input
        prefix-icon="el-icon-user"
        type="text"
        auto-complete="false"
        v-model="loginForm.nickname"
        placeholder="请输入昵称"
      ></el-input>
    </el-form-item>
    <el-form-item prop="email">
      <el-input
        prefix-icon="el-icon-message"
        type="text"
        auto-complete="false"
        v-model="loginForm.email"
        placeholder="请输入邮箱"
      ></el-input>
    </el-form-item>
    <el-form-item prop="introduction">
      <el-input
        prefix-icon="el-icon-eleme"
        type="text"
        auto-complete="false"
        v-model="loginForm.introduction"
        placeholder="请输入简介"
      ></el-input>
    </el-form-item>
    <el-button type="primary" style="width: 100%" @click="submitLogin"
      >注册</el-button
    >
  </el-form>
</template>

<script>
export default {
  name: "Regist",
  data() {
    return {
      loginForm: {
        username: "",
        password: "",
        nickname: "",
        email: "",
        introduction: "",
      },
      loading: false,
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            min: 4,
            max: 30,
            message: "长度在 4 到 30 个字符",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 2,
            max: 30,
            message: "长度在 2 到 30 个字符",
            trigger: "blur",
          },
        ],
        nickname: { required: true, message: "请输入昵称", trigger: "blur" },
        email: [
          { required: true, message: "请输入邮箱", trigger: "blur" },
          {
            type: "email",
            message: "请输入正确的邮箱地址",
            trigger: ["blur", "change"],
          },
        ],
      },
    };
  },
  methods: {
    submitLogin() {
      this.$refs["loginForm"].validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$post("user/add", this.loginForm).then((res) => {
            if (res.code == 200) {
              this.loading = false;
              let path = this.$route.query.redirect;
              setTimeout(
                this.$router.replace(
                  path == "/" || path == undefined ? "/login" : path
                ),
                2000
              );
            }
          });
        } else {
          this.$message.error("请输入必填字段");
          return false;
        }
      });
    },
  },
};
</script>

<style>
.loginContainer {
  border-radius: 15px;
  background-clip: padding-box;
  margin: 180px auto;
  width: 350px;
  padding: 15px 35px;
  background: #fff;
  border: 1px solid #eaeaea;
  box-shadow: 0 0 25px #cac6c6;
}
.loginTitle {
  margin: 10px auto 30px auto;
  text-align: center;
}
.loginRemember {
  float: left;
  margin: 0 0 15px 0;
}
.el-form-item__content {
  display: flex;
  align-items: center;
}
</style>